function dohangqing(code) {
    $('#hangqing_iframe').attr('src', hq_url + 'html5/index.html?t=' + code + '&i=0');
    $('#hqcode').val(code);
    gethq();
}
function gethq() {
    var code = $('#hqcode').val();
    if (code) {
        var i = getRandom(100);
        $.ajax({
            type: "get",
            async: false, //同步请求
            dataType: "jsonp",
            url: jinfu_url + '/ajax/gethq',
            data: {code: code, i: i},
            success: function (json) {
                if (json.status != -1) {
                    $('#sell').html(json.sell);
                    $('#hqname').html(json.name);
                  //  $('#hqname').attr(jinfu_url+'/'+json.href);
                    if (json.num > 0) {
                        $('#sd').attr('class', 'left sp_b bg1');
                        $('#bfb').css('color', 'red');
                        $('#num').css('color', 'red');
                    } else {
                        $('#sd').attr('class', 'left sp_b bg2');
                        $('#bfb').css('color', 'green');
                        $('#num').css('color', 'green');
                    }
                    $('#bfb').html(json.bfb + '%');
                    $('#num').html(json.num);
                }
            }


        })

    }
}
function getRandom(n) {
    return Math.floor(Math.random() * n + 1)
}
setInterval("gethq()", 10000);
$(document).ready(function () {
    dohangqing('OIL10');
    $(".products").change(function () {
        dohangqing(this.value);
    })

 
    $.fn.Xslider = function (options) {
        var settings = {
            affect: 'scrollx', //效果  有scrollx|scrolly|fade|none
            speed: 1200, //动画速度
            space: 6000, //时间间隔
            auto: true, //自动滚动
            trigger: 'mouseover', //触发事件 注意用mouseover代替hover
            conbox: '.conbox', //内容容器id或class
            ctag: 'a', //内容标签 默认为<a>
            switcher: '.switcher', //切换触发器id或class
            stag: 'a', //切换器标签 默认为a
            current: 'cur', //当前切换器样式名称
            rand: false //是否随机指定默认幻灯图片
        };
        settings = $.extend({}, settings, options);
        var index = 1;
        var last_index = 0;
        var $conbox = $(this).find(settings.conbox), $contents = $conbox.find(settings.ctag);
        var $switcher = $(this).find(settings.switcher), $stag = $switcher.find(settings.stag);
        if (settings.rand) {
            index = Math.floor(Math.random() * $contents.length);
            slide();
        }
        if (settings.affect == 'fade') {
            $.each($contents, function (k, v) {
                (k === 0) ? $(this).css({'position': 'absolute', 'z-index': 9}) : $(this).css({'position': 'absolute', 'z-index': 1, 'opacity': 0});
            });
        }
        function slide() {
            if (index >= $contents.length)
                index = 0;
            $stag.removeClass(settings.current).eq(index).addClass(settings.current);
            switch (settings.affect) {
                case 'scrollx':
                    $conbox.width($contents.length * $contents.width());
                    $conbox.stop().animate({left: -$contents.width() * index}, settings.speed);
                    break;
                case 'scrolly':
                    $contents.css({display: 'block'});
                    $conbox.stop().animate({top: -$contents.height() * index + 'px'}, settings.speed);
                    break;
                case 'fade':
                    $contents.eq(last_index).stop().animate({'opacity': 0}, settings.speed / 2).css('z-index', 1)
                            .end()
                            .eq(index).css('z-index', 9).stop().animate({'opacity': 1}, settings.speed / 2)
                    break;
                case 'none':
                    $contents.hide().eq(index).show();
                    break;
            }
            last_index = index;
            index++;
        }
        ;
        if (settings.auto)
            var Timer = setInterval(slide, settings.space);
        $stag.bind(settings.trigger, function () {
            _pause()
            index = $(this).index();
            slide();
            _continue()
        });
        $conbox.hover(_pause, _continue);
        function _pause() {
            clearInterval(Timer);
        }
        function _continue() {
            if (settings.auto)
                Timer = setInterval(slide, settings.space);
        }
    }
 
    // 头部banner水平滚动
    $("#slider1").Xslider({
        // 默认配置
        affect: 'fade', //效果  有scrollx|scrolly|fade|none
        speed: 800, //动画速度
        space: 6000, //时间间隔
        auto: true, //自动滚动
        trigger: 'mouseover', //触发事件 注意用mouseover代替hover
        conbox: '.conbox', //内容容器id或class
        ctag: 'div', //内容标签 默认为<a>
        switcher: '.switcher', //切换触发器id或class
        stag: 'a', //切换器标签 默认为a
        current: 'cur', //当前切换器样式名称
        rand: false //是否随机指定默认幻灯图片
    });


    // 图片渐隐
    $("#slider2").Xslider({
        // 默认配置
        affect: 'fade', //效果  有scrollx|scrolly|fade|none
        speed: 1200, //动画速度
        space: 4000, //时间间隔
        auto: true, //自动滚动
        trigger: 'mouseover', //触发事件 注意用mouseover代替hover
        conbox: '.conbox2', //内容容器id或class
        ctag: 'div', //内容标签 默认为<a>
        switcher: '.switcher2', //切换触发器id或class
        stag: 'a', //切换器标签 默认为a
        current: 'cur', //当前切换器样式名称
        rand: false //是否随机指定默认幻灯图片
    });
 
    function G(s) {
        return document.getElementById(s);
    }
    function getStyle(obj, attr) {
        if (obj.currentStyle) {
            return obj.currentStyle[attr];
        } else {
            return getComputedStyle(obj, false)[attr];
        }
    }
    function Animate(obj, json) {
        if (obj.timer) {
            clearInterval(obj.timer);
        }
        obj.timer = setInterval(function () {
            for (var attr in json) {
                var iCur = parseInt(getStyle(obj, attr));
                iCur = iCur ? iCur : 0;
                var iSpeed = (json[attr] - iCur) / 6;
                iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
                obj.style[attr] = iCur + iSpeed + 'px';
                if (iCur == json[attr]) {
                    clearInterval(obj.timer);
                }
            }
        }, 30);
    }

    var oPic = G("picBox");
    var oList = G("listBox");

    var oPrev = G("prev");
    var oNext = G("next");
    var oPrevTop = G("prevTop");
    var oNextTop = G("nextTop");

    var oPicLi = oPic.getElementsByTagName("li");
    var oListLi = oList.getElementsByTagName("li");
    var len1 = oPicLi.length;
    var len2 = oListLi.length;

    var oPicUl = oPic.getElementsByTagName("ul")[0];
    var oListUl = oList.getElementsByTagName("ul")[0];
    var w1 = oPicLi[0].offsetWidth;
    var w2 = oListLi[0].offsetWidth;

    oPicUl.style.width = w1 * len1 + "px";
    oListUl.style.width = w2 * len2 + "px";

    var index = 0;

    var num = 6;
    var num2 = Math.ceil(num / 2);

    function Change() {

        Animate(oPicUl, {left: -index * w1});

        if (index < num2) {
            Animate(oListUl, {left: 0});
        } else if (index + num2 <= len2) {
            Animate(oListUl, {left: -(index - num2 + 1) * w2});
        } else {
            Animate(oListUl, {left: -(len2 - num) * w2});
        }

        for (var i = 0; i < len2; i++) {
            oListLi[i].className = "";
            if (i == index) {
                oListLi[i].className = "on";
            }
        }
    }
    oNextTop.onclick = oNext.onclick = function () {
        index++;
        index = index == len2 ? 0 : index;
        Change();
    }
    oPrevTop.onclick = oPrev.onclick = function () {
        index--;
        index = index == -1 ? len2 - 1 : index;
        Change();
    }
    for (var i = 0; i < len2; i++) {
        oListLi[i].index = i;
        oListLi[i].onclick = function () {
            index = this.index;
            Change();
        }
    }
 
    $.fn.kxbdMarquee = function (options) {
        var opts = $.extend({}, $.fn.kxbdMarquee.defaults, options);

        return this.each(function () {
            var $marquee = $(this);//滚动元素容器
            var _scrollObj = $marquee.get(0);//滚动元素容器DOM
            var scrollW = $marquee.width();//滚动元素容器的宽度
            var scrollH = $marquee.height();//滚动元素容器的高度
            var $element = $marquee.children(); //滚动元素
            var $kids = $element.children();//滚动子元素
            var scrollSize = 0;//滚动元素尺寸
            var _type = (opts.direction == 'left' || opts.direction == 'right') ? 1 : 0;//滚动类型，1左右，0上下

            //防止滚动子元素比滚动元素宽而取不到实际滚动子元素宽度
            $element.css(_type ? 'width' : 'height', 10000);
            //获取滚动元素的尺寸
            if (opts.isEqual) {
                scrollSize = $kids[_type ? 'outerWidth' : 'outerHeight']() * $kids.length;
            } else {
                $kids.each(function () {
                    scrollSize += $(this)[_type ? 'outerWidth' : 'outerHeight']();
                });
            }
            //滚动元素总尺寸小于容器尺寸，不滚动
            if (scrollSize < (_type ? scrollW : scrollH))
                return;
            //克隆滚动子元素将其插入到滚动元素后，并设定滚动元素宽度
            $element.append($kids.clone()).css(_type ? 'width' : 'height', scrollSize * 2);

            var numMoved = 0;
            function scrollFunc() {
                var _dir = (opts.direction == 'left' || opts.direction == 'right') ? 'scrollLeft' : 'scrollTop';
                if (opts.loop > 0) {
                    numMoved += opts.scrollAmount;
                    if (numMoved > scrollSize * opts.loop) {
                        _scrollObj[_dir] = 0;
                        return clearInterval(moveId);
                    }
                }
                if (opts.direction == 'left' || opts.direction == 'up') {
                    var newPos = _scrollObj[_dir] + opts.scrollAmount;
                    if (newPos >= scrollSize) {
                        newPos -= scrollSize;
                    }
                    _scrollObj[_dir] = newPos;
                } else {
                    var newPos = _scrollObj[_dir] - opts.scrollAmount;
                    if (newPos <= 0) {
                        newPos += scrollSize;
                    }
                    _scrollObj[_dir] = newPos;
                }
            }
            ;
            //滚动开始
            var moveId = setInterval(scrollFunc, opts.scrollDelay);
            //鼠标划过停止滚动
            $marquee.hover(
                    function () {
                        clearInterval(moveId);
                    },
                    function () {
                        clearInterval(moveId);
                        moveId = setInterval(scrollFunc, opts.scrollDelay);
                    }
            );

            //控制加速运动
            if (opts.controlBtn) {
                $.each(opts.controlBtn, function (i, val) {
                    $(val).bind(opts.eventA, function () {
                        opts.direction = i;
                        opts.oldAmount = opts.scrollAmount;
                        opts.scrollAmount = opts.newAmount;
                    }).bind(opts.eventB, function () {
                        opts.scrollAmount = opts.oldAmount;
                    });
                });
            }
        });
    };
    $.fn.kxbdMarquee.defaults = {
        isEqual: true, //所有滚动的元素长宽是否相等,true,false
        loop: 0, //循环滚动次数，0时无限
        direction: 'left', //滚动方向，'left','right','up','down'
        scrollAmount: 1, //步长
        scrollDelay: 30, //时长
        newAmount: 3, //加速滚动的步长
        eventA: 'mousedown', //鼠标事件，加速
        eventB: 'mouseup'//鼠标事件，原速
    };

    $.fn.kxbdMarquee.setDefaults = function (settings) {
        $.extend($.fn.kxbdMarquee.defaults, settings);
    };

 
    //首页视频节目预告滚
    $('.marquee').kxbdMarquee({
        direction: 'up',
        eventA: 'mouseenter',
        eventB: 'mouseleave'
    });


});